package controller.admin;

import java.io.IOException;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import pojo.Hoadon;
import dao.HoadonDAO;

/**
 * Servlet implementation class Header
 */
@WebServlet("/Pages/TrangQuanLyDonDatHang.do")
public class TrangQuanLyDonDatHang extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public TrangQuanLyDonDatHang() {
		super();
	}

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		processRequest(request, response);
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		processRequest(request, response);
	}

	protected void processRequest(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html;charset=UTF-8");

		HttpSession session = request.getSession();

		String action = request.getParameter("action");
		String url = "";
		
		if (action.equals("TrangQuanLyDonDatHang")) {

			request.setAttribute("page", "TrangQuanLyDonDatHang");
			url = "../index.jsp";
		}
		
		if (action.equals("LietKeToanBo")) {

			HoadonDAO hddao = new HoadonDAO();
			List<Hoadon> lstHoaDon = hddao.getList();

			// Ép các hóa đơn tự tính tôngr tiền (vì chưa có cột tổng tiền)
			for (int i = 0; i < lstHoaDon.size(); i++)
				lstHoaDon.get(i).TinhTongTien();

			request.setAttribute("listHoaDon", lstHoaDon);

			request.setAttribute("page", "TrangQuanLyDonDatHang");
			url = "../index.jsp";
		}
		//------------Button----------
		if (action.equals("Xoa")) {

			String maHoaDon = request.getParameter("maHoaDon");
			
			// ĐỢi xử lý sau khi đã thêm cột vào trong CSDL

			
			request.setAttribute("page", "TrangQuanLyDonDatHang");
			url = "../index.jsp";
		}
		if (action.equals("Sua")) {

			String maHoaDon = request.getParameter("maHoaDon");

			// Xử lý sau
			
			request.setAttribute("page", "TrangQuanLyDonDatHang");
			url = "../index.jsp";
		}
		if (action.equals("Duyet")) {

			String maHoaDon = request.getParameter("maHoaDon");

			HoadonDAO hddao = new HoadonDAO();
			Hoadon hoadon = hddao.get(Integer.valueOf(maHoaDon));
			
			hoadon.setDaduyet(true);
			hddao.saveOrUpdate(hoadon);
			
			request.setAttribute("page", "TrangQuanLyDonDatHang");
			url = "../index.jsp";
		}
		//------------Button----------

		RequestDispatcher rd = request.getRequestDispatcher(url);
		rd.forward(request, response);
	}
}
